/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.rw.magallanes.ide.commons.search;

import com.rw.magallanes.core.Category;
import com.rw.magallanes.core.Item;
import com.rw.magallanes.core.Warehouse;
import com.rw.magallanes.ide.controller.ItemController;
import com.rw.magallanes.ide.main.MainSystem;
import com.rw.table.APropertyTableModel;
import com.rw.table.ATableAction;
import com.rw.table.editor.ComboCellEditor;
import com.rw.table.editor.ICellEditor;
import com.rw.table.editor.TextCellEditor;
import com.rw.table.formatter.DoubleCellFormatter;
import com.rw.table.formatter.ICellFormatter;
import com.rw.table.formatter.IntegerCellFormatter;
import com.rw.table.formatter.StringCellFormatter;
import com.rw.table.render.ICellRenderer;
import com.rw.table.render.LabelCellRenderer;

/**
 *
 * @author Gonzalez
 */
public class AddItemTableModel extends APropertyTableModel implements AddItemContants {

    private Warehouse wh = MainSystem.getInstance().getMainFrame().getWarehouse();
    Category[] categoryArray = wh.getCategoriesArray();
    private LabelCellRenderer labelRenderer = new LabelCellRenderer();
    private TextCellEditor textEditor = new TextCellEditor();
    private ComboCellEditor comboEditor = new ComboCellEditor(categoryArray);
    private StringCellFormatter strFormatter = new StringCellFormatter();
    private DoubleCellFormatter dblFormatter = new DoubleCellFormatter(2);
    private IntegerCellFormatter intFormatter = new IntegerCellFormatter();
    //
    private Item item;

    public AddItemTableModel(String name) {
        this.item = new Item(name, categoryArray[0], 0, 0);
    }

    @Override
    public ICellRenderer getPropertyRenderer(int row) {
        return labelRenderer;
    }

    @Override
    public ICellEditor getPropertyEditor(int row) {
        switch (row) {
            case ROW_NAME:
                return textEditor;
            case ROW_CATEGORY:
                return comboEditor;
            case ROW_PRICE:
                return textEditor;
            case ROW_COUNT:
                return textEditor;
            default:
                throw new RuntimeException("Fila desconocida: " + row);
        }
    }

    @Override
    public ICellFormatter getPropertyFormatter(int row) {
        switch (row) {
            case ROW_NAME:
                return strFormatter;
            case ROW_CATEGORY:
                return strFormatter;
            case ROW_PRICE:
                return dblFormatter;
            case ROW_COUNT:
                return intFormatter;
            default:
                throw new RuntimeException("Fila desconocida: " + row);
        }
    }

    @Override
    public String getPropertyName(int row) {
        return rowNames[row];
    }

    @Override
    public Object getPropertyValue(int row) {
        switch (row) {
            case ROW_NAME:
                return item.getName();
            case ROW_CATEGORY:
                return item.getCategory();
            case ROW_PRICE:
                return item.getPrice();
            case ROW_COUNT:
                return item.getCount();
            default:
                throw new RuntimeException("Fila desconocida: " + row);
        }
    }

    @Override
    public void setPropertyValue(Object value, int row) {
        if (value == null) {
            return;
        }
        switch (row) {
            case ROW_NAME:
                ItemController.setName(item, (String) value);
                break;
            case ROW_CATEGORY:
                ItemController.setCategory(item, (Category) value);
                break;
            case ROW_PRICE:
                ItemController.setPrice(item, ((Double) value).doubleValue());
                break;
            case ROW_COUNT:
                ItemController.setCount(item, ((Integer) value).intValue());
                break;
            default:
                throw new RuntimeException("Fila desconocida: " + row);
        }
    }

    @Override
    public ATableAction getPropertyEditableByClickAction(int row) {
        return null;
    }

    @Override
    public int getRowCount() {
        return rowNames.length;
    }

    Item getItem() {
        return item;
    }
}
